package com.example.customviewtest.clipImage;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.customviewtest.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ShowImageActivity extends AppCompatActivity implements BaseQuickAdapter.OnItemClickListener {

    private RecyclerView mRecycleview;
    List<String> img_list = new ArrayList<>();

    private static final String outUri = Environment.getExternalStorageDirectory() + "Customview/clip/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_image);
        scanImg();
        mRecycleview = findViewById(R.id.recycleview);
        mRecycleview.setLayoutManager(new GridLayoutManager(this, 4));
        ImgAdapter imgAdapter = new ImgAdapter(this, R.layout.show_recycler_item, img_list);
        mRecycleview.setAdapter(imgAdapter);
        imgAdapter.setOnItemClickListener(this);

    }

    /**
     * 扫描图片
     */
    public void scanImg() {
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(
                uri,
                null,
                MediaStore.Images.Media.MIME_TYPE + "=\"image/jpeg\" or " +
                        MediaStore.Images.Media.MIME_TYPE + "=\"image/png\"",
                null,
                MediaStore.Images.Media.DATE_MODIFIED + " desc"
        );
        String path = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Log.i("xd---", cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                    path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                } else {
                    Long id = cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                    Uri uri1 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                    path = uri1.toString();
                }
                File file = new File(path);
                if (file.exists()) {
                    img_list.add(path);
                }
            }
            cursor.close();
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        File file = new File(outUri);
        if (!file.exists()) {
            file.mkdirs();
        }
        Intent intent = new Intent(ShowImageActivity.this, ClipImageActivity.class);
        intent.putExtra("Uri", img_list.get(position));
        startActivity(intent);
    }
}
